using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using UnityEngine;
namespace xingzhimeng
{
  ///<summary>
  ///
  ///</summary>
public class GameManeger : MonoBehaviour
  {
    private static GameManeger insnter;
    [HideInInspector]
    public static GameManeger Insnter
    {
      get { return insnter;}
      private set { insnter = value; }
    }
    private BirdParent[] birds;//小鸟集合
    private GameObject[] pigs;//猪
    public List<BirdParent> birdList;
    public List<GameObject> pigList;
    public Transform initanPoint;//初始位置
    private Vector3 TopPoint; //上一只鸟的位置
        public float speed;
        public int BirdMax;
        

        private void Awake()
   {
     insnter = this;
     
   }

   private void Start()
   {
     InitsList();
   }

   void InitsList()
   {
   
     

     birds = FindObjectsOfType<BirdParent>();
            pigs = GameObject.FindGameObjectsWithTag("Enemy");
    

     birdList=  birds.ToList();
     pigList = pigs.ToList();
     birdList.Sort();
     for (int i = 0; i < birdList.Count; i++)
     {
               
       birdList[i].GetComponent<SpringJoint2D>().enabled = (i==0);
       birdList[i].enabled = (i==0);
     }
            BirdMax = birdList.Count;
             




        }

   public void NextBird(BirdParent bir)
   {
     birdList.Remove(bir);
     if (pigList.Count > 0)
     {
       if (birdList.Count > 0)
       {
                    //换鸟
                    TopPoint = birdList[0].transform.position;
                  MoveInitPoint();
                    if (birdList.Count > 1)
                    {
                        //把后面的小鸟向前排                        
                    StartCoroutine(MoveTopPoint());

                    }

                 
                }
       else
       {
         //游戏结束
         print("游戏失败");
                    //   GameOver.SetActive(true);
                    UIManager.Instance.SetVisible(UIName.GameOver, true);
       }
       
     }else
     {
       //游戏胜利
       print("胜利");
                StartCoroutine(GameWins());
     }
   }
        //游戏胜利方法
        IEnumerator GameWins()
        {
            for(int i = 0; i < birdList.Count; i++)
            {
              GameObject score=  Instantiate((birdList[i] as Birds).GameWinScore, birdList[i].transform.position +
                    new Vector3(0, 0.6f, 0), Quaternion.identity);
                Destroy(score, 1.5f);
                yield return new WaitForSeconds(0.7f);
            }
            //  GameWin.SetActive(true);
            UIManager.Instance.SetVisible(UIName.Win, true);
        }

        IEnumerator MoveTopPoint()
        {
            while (true)
            {
                if (Vector3.Distance(birdList[1].transform.position, TopPoint) < 0.1)
                {
                    break;
                }
                birdList[1].transform.position = Vector3.MoveTowards(birdList[1].transform.position
                    , TopPoint, speed * Time.deltaTime);
                yield return null;
            }
            birdList[1].transform.position = TopPoint;

        }
        //鸟移动到初始位置
        private void MoveInitPoint()
        {
            
            birdList[0].transform.position = initanPoint.position;
            birdList[0].GetComponent<SpringJoint2D>().enabled = true;
            birdList[0].enabled = true;

        }

   public void PigDead(Pigs pig)
   {
     pigList.Remove(pig.gameObject);
   }
  }

}

